#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
import sys

from flask import render_template, request, jsonify
from app import commom, PAGE_NUM, page_code, upload_files
from app.util import admin_login
from dbConnection.MysqlConn import Mysql
from . import admin

reload(sys)
sys.setdefaultencoding('utf8')


# 分类列表
@admin.route('/admin/classify_list.html')
@admin_login
def classify_list():
    sub = commom.get_subject()
    return render_template('admin/course/classify_list.html', sub=sub)


# 获取列表数据
@admin.route('/admin/classify_list_post', methods=['post'])
@admin_login
def classify_list_post():
    parms = ['cname', 'sid']
    maps = {}
    for d in parms:
        maps[d] = request.values.get(d)
    p = request.values.get('p', '1')
    c_num = (int(p) - 1)*PAGE_NUM
    sql = "select t.*, s.typeName FROM girl_type AS t LEFT JOIN girl_subject AS s ON s.sid = t.sid WHERE 1=1"
    sql2 = "select count(t.tid) AS con FROM girl_type AS t LEFT JOIN girl_subject AS s ON s.sid = t.sid WHERE 1=1"
    if maps['cname']:
        sql += " and t.tid = '" + maps['cname'] + "' or locate('" + maps['cname'] + "', t.courseName)>0"
        sql2 += " and t.tid = '" + maps['cname'] + "' or locate('" + maps['cname'] + "', t.courseName)>0"
    if maps['sid']:
        sql += " and t.sid = '%s'" % maps['sid']
        sql2 += " and t.sid = '%s'" % maps['sid']
    sql += " order by t.tid desc limit %d, %d" % (c_num, PAGE_NUM)
    mysql = Mysql()
    res = mysql.getAll(sql, None)
    cont = mysql.getOne(sql2, None)
    mysql.dispose()
    pages = int(math.ceil(cont['con'] / float(PAGE_NUM))) if cont else 0
    route_path = 'get_data'
    html_str = page_code.page_code_html_post(int(p), route_path, pages) if pages > 0 else ''
    if res:
        for d in res:
            d['addTime'] = str(d['addTime'])
        data = {
            'status':1,
            'res':res,
            'page':html_str,
            'p':p
        }
    else:
        data = {
            'status':2,
            'page':html_str
        }
    return jsonify(data)


# 更新分类状态
@admin.route('/admin/update_classify_post', methods=['post'])
@admin_login
def update_classify_post():
    tid = request.values.get('tid')
    pid = request.values.get('pid')
    if pid == '1':
        sql = "update girl_type set status = 1 WHERE tid = '%s'" % tid
    elif pid == '2':
        sql = "update girl_type set status = 0 WHERE tid = '%s'" % tid
    elif pid == '3':
        sql = "delete FROM girl_type WHERE tid = '%s'" % tid
    else:
        return jsonify({'status':0})
    mysql = Mysql()
    res = mysql.update(sql, None)
    mysql.dispose()
    if res > 0:
        data = {
            'status':1
        }
    else:
        data = {
            'status':0
        }
    return jsonify(data)


# 新增分类
@admin.route('/admin/classify_add.html')
@admin_login
def classify_add():
    sub = commom.get_subject()
    return render_template('admin/course/classify_add.html', sub=sub)


# 新增分类处理
@admin.route('/admin/classify_add_post', methods=['post'])
@admin_login
def classify_add_post():
    cname = request.values.get('cname')
    sid = request.values.get('sid')
    sql = "insert INTO girl_type(courseName, sid, addTime) VALUES ('%s', '%s', now())" % (cname, sid)
    mysql = Mysql()
    res = mysql.insertOne(sql, None)
    mysql.dispose()
    if res > 0:
        data = {
            'status':1
        }
    else:
        data = {
            'status':0
        }
    return jsonify(data)


# 编辑分类
@admin.route('/admin/classify_detail.html')
@admin_login
def classify_detail():
    tid = request.values.get('tid')
    mysql = Mysql()
    sql = "select t.*, s.typeName FROM girl_type AS t LEFT JOIN girl_subject AS s ON s.sid = t.sid WHERE " \
          "t.tid = '%s'" % tid
    res = mysql.getOne(sql, None)
    mysql.dispose()
    return render_template('admin/course/classify_detail.html', res=res)


# 修改分类名
@admin.route('/admin/classify_detail_post', methods=['post'])
@admin_login
def classify_detail_post():
    cname = request.values.get('cname')
    tid = request.values.get('tid')
    sql = "update girl_type SET courseName = '%s' WHERE tid = '%s'" % (cname, tid)
    mysql = Mysql()
    res = mysql.update(sql, None)
    mysql.dispose()
    if res > 0:
        data = {
            'status':1
        }
    else:
        data = {
            'status':0
        }
    return jsonify(data)
